package com.finchina.myview.viewgroups;

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * 作者：黄赛赛 on 2015/9/11 11:27
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class LeftDrawerLayout1 extends LinearLayout {

    ViewDragHelper dragHelper;
    View firstView, secondView;

    int divideX =0 ;
    boolean isOpen = false;
    public LeftDrawerLayout1(Context context) {
        this(context, null);
    }

    public LeftDrawerLayout1(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LeftDrawerLayout1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        dragHelper = ViewDragHelper.create(this, 1.0f, dragCallBack);
        dragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }

    ViewDragHelper.Callback dragCallBack = new ViewDragHelper.Callback(){

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return child == secondView;
        }

        @Override
        public int getViewVerticalDragRange(View child) {
            return child.getMeasuredWidth();
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return super.getViewHorizontalDragRange(child);
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
          Log.e("divideX", "divideX = " + divideX);
            if (releasedChild == secondView){
                int left = secondView.getLeft();
                Log.e("left", "left = " + left);
                if (left > divideX){
                    dragHelper.settleCapturedViewAt(0, 0);
                    isOpen = true;
                }else {
                    dragHelper.settleCapturedViewAt(-secondView.getWidth(), 0);
                    isOpen = false;
                }
                invalidate();
            }

        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            return left < 0 ? left : 0;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            return super.clampViewPositionVertical(child, top, dy);
        }

        @Override
        public void onEdgeDragStarted(int edgeFlags, int pointerId) {
//            super.onEdgeDragStarted(edgeFlags, pointerId);
                dragHelper.captureChildView(secondView, pointerId);

        }


        @Override
        public void onEdgeTouched(int edgeFlags, int pointerId) {
            super.onEdgeTouched(edgeFlags, pointerId);
        }

        @Override
        public void onViewCaptured(View capturedChild, int activePointerId) {
            super.onViewCaptured(capturedChild, activePointerId);
        }


    };

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int cWidth = secondView.getMeasuredWidth();
        if (divideX == 0 ){
            divideX = (int) (-cWidth/2.0);
        }

        secondView.layout(-cWidth,t,0,b);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        return super.onTouchEvent(event);
        dragHelper.processTouchEvent(event);

        return  true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return dragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        secondView = getChildAt(1);

    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (dragHelper.continueSettling(true)){
            invalidate();
        }
    }



}
